<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true">
      <el-form-item>
        <el-autocomplete
          :fetch-suggestions="querySearch"
          :trigger-on-focus="false"
          v-model="searchObj.name"
          size="small"
          value-key="name"
          class="inline-input"
          placeholder="请输入讲师名"
        />
      </el-form-item>

      <el-form-item>
        <el-select v-model="searchObj.level" clearable placeholder="头衔" size="small">
          <el-option value="1" label="高级讲师"/>
          <el-option value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>

      <el-form-item label="入驻时间">
        <el-date-picker
          v-model="searchObj.joinDateBegin"
          size="small"
          placeholder="开始时间"
          value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="-">
        <el-date-picker
          v-model="searchObj.joinDateEnd"
          size="small"
          placeholder="结束时间"
          value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" icon="el-icon-search" @click="getList">查询</el-button>
        <el-button type="default" size="small" @click="resetData()">清空</el-button>
      </el-form-item>
    </el-form>

    <!--工具条-->
    <div style="margin-bottom: 20px">
      <el-button type="danger" size="mini" @click="batchRemove">批量删除</el-button>
    </div>

    <!--讲师列表-->
    <el-table :data="list" border stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50"/>
      <el-table-column label="序号" width="50" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" width="80" align="center"/>
      <el-table-column label="头衔" width="90" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.level === 1" type="success" size="mini">高级讲师</el-tag>
          <el-tag v-if="scope.row.level === 2" size="mini">首席讲师</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="intro" label="简介" />
      <el-table-column prop="sort" align="center" label="排序" width="60" />
      <el-table-column prop="joinDate" label="入驻时间" width="160" align="center"/>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <router-link :to="'/teacher/edit/' + scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center"
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="changeCurrentPage"
      @size-change="changePageSize"
    />
  </div>
</template>

<script>

import teacher from '@/api/teacher'

export default {
  data() {
    return {
      list: [],
      total: 0,
      page: 1,
      limit: 10,
      searchObj: {},
      mutltipleSelection: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      teacher.pageList(this.page, this.limit, this.searchObj).then(res => {
        this.list = res.data.rows
        this.total = res.data.total
      })
    },
    // 每页显示数据变化时
    changePageSize(size) {
      this.limit = size
      this.getList()
    },
    // 分页页码变更时
    changeCurrentPage(page) {
      this.page = page
      this.getList()
    },
    // 重置查询条件
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    // 删除
    handleDelete(id) {
      // 询问是否删除
      this.$confirm('此操作将会删除该讲师，是否继续？', '警告', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        teacher.removeById(id).then(result => {
          // 刷新页面
          this.getList()
          this.$message.success(result.message)
        })
      }).catch(err => {
        if (err === 'cancel') {
          this.$message({
            type: 'info',
            message: '取消删除'
          })
        }
      })
    },
    // 批量删除
    batchRemove() {
      // 先判是否选中数据
      if (this.mutltipleSelection.length <= 0) {
        return this.$message({
          type: 'warning',
          message: '请选择需要删除的数据'
        })
      }

      // 询问是否删除
      this.$confirm('此操作将会删除选中数据，是否继续？', '警告', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = []
        this.mutltipleSelection.forEach(item => ids.push(item.id))
        teacher.removeBatch(ids).then(result => {
          // 刷新页面
          this.getList()
          this.$message.success(result.message)
        })
      }).catch(err => {
        if (err === 'cancel') {
          this.$message({
            type: 'info',
            message: '取消删除'
          })
        }
      })
    },
    // 当表格多选项发生变化时，触发
    handleSelectionChange(selection) {
      this.mutltipleSelection = selection
    },
    // 输入提示
    querySearch(queryString, callback) {
      teacher.selectNameListByKey(queryString).then(res => {
        // 调用callback将获取的数据传入该方法中
        callback(res.data.items)
      })
    }
  }
}
</script>

<style lang="scss" rel="stylesheet/scss">

</style>
